package com.basker.pisces.core.query;

import java.text.MessageFormat;

import com.basker.pisces.core.query.meta.FieldDesc;

/**
 * 查询字段
 *
 * @author hangwen
 */
public class SelectField {
    private String fieldId;
    private String alias;

    public SelectField() {

    }

    /**
     * 通过{@link FieldDesc}创建
     *
     * @param fieldDesc
     */
    public SelectField(FieldDesc fieldDesc) {
        this(fieldDesc.getId(), fieldDesc.getAlias());
    }

    /**
     * id和别名一致
     *
     * @param id
     */
    public SelectField(String id) {
        this(id, id);
    }

    /**
     * 具体指定id和别名
     *
     * @param id
     * @param alias
     */
    public SelectField(String id, String alias) {
        this.fieldId = id;
        this.alias = alias;
    }

    public String genSelectExpression(FieldDesc fieldDesc) {
        String tableAlias = fieldDesc.getHostTable().getAlias();
        String fieldName = fieldDesc.getName();

        return MessageFormat.format("{0}.{1}", tableAlias, fieldName);
    }

    /**
     * 字段别名，同一个查询器中查询字段唯一
     *
     * @return
     */
    public String getAlias() {
        return alias;
    }

    public void setAlias(String alias) {
        this.alias = alias;
    }

    /**
     * 字段Id,关联{@link FieldDesc#getId()}
     *
     * @return
     */
    public String getFieldId() {
        return fieldId;
    }

    public void setFieldId(String fieldId) {
        this.fieldId = fieldId;
    }

    @Override
    public String toString() {
        return this.getAlias();
    }
}
